import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

// // 解决Vue-Router升级导致的Uncaught(in promise) navigation guard问题
// // 路由守卫重定向报错问题
// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function push(location, onResolve, onReject) {
//   if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
//   return originalPush.call(this, location).catch(err => err)// 增加catch捕获异常
// }

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: () => import('@/views/Index.vue')
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import('@/views/Category.vue')
  },
  {
    path: '/productlist',
    name: 'ProductList',
    meta: {
      hideTab: true
    },
    component: () => import('@/views/ProductList.vue')
  },
  {
    path: '/detail/:id',
    name: 'Detail',
    props: true,
    meta: {
      hideTab: true,
      notKeepAlive: true
    },
    component: () => import('@/views/Detail.vue')
  },
  {
    path: '/cart',
    name: 'Cart',
    meta: {
      login: true
    },
    component: () => import('@/views/Cart.vue')
  },
  {
    path: '/order/confirm/:cartId',
    name: 'OrderConfirm',
    props: true,
    meta: {
      login: true,
      hideTab: true,
    },
    component: () => import('@/views/OrderConfirm.vue')
  },
  {
    path: '/address/edit',
    name: 'AddressEdit',
    meta: {
      login: true,
      hideTab: true,
    },
    component: () => import('@/views/AddressEdit.vue')
  },
  {
    path: '/address/list',
    name: 'AddressList',
    meta: {
      login: true,
      hideTab: true,
    },
    component: () => import('@/views/AddressList.vue')
  },
  {
    path: '/order/status/:orderId',
    name: 'OrderStatus',
    props: true,
    meta: {
      login: true,
      hideTab: true,
      notKeepAlive: true
    },
    component: () => import('@/views/OrderStatus.vue')
  },
  {
    path: '/my',
    name: 'My',
    meta: {
      login: true
    },
    component: () => import('@/views/My.vue')
  },
  {
    path: '/collect/list',
    name: 'CollectList',
    meta: {
      login: true,
      hideTab: true,
    },
    component: () => import('@/views/CollectList.vue')
  },
  {
    path: '/order/list/:orderType',
    name: 'OrderList',
    props: true,
    meta: {
      login: true,
      hideTab: true,
      notKeepAlive: true
    },
    component: () => import('@/views/OrderList.vue')
  },
  {
    path: '/order/detail/:orderId',
    name: 'OrderDetail',
    props: true,
    meta: {
      login: true,
      hideTab: true,
      notKeepAlive: true
    },
    component: () => import('@/views/OrderDetail.vue')
  },
  {
    path: '/order/refund/:orderId',
    name: 'OrderRefund',
    props: true,
    meta: {
      login: true,
      hideTab: true,
      notKeepAlive: true
    },
    component: () => import('@/views/OrderRefund.vue')
  },
  {
    path: '/my/user',
    name: 'UserData',
    meta: {
      login: true,
      hideTab: true,
      notKeepAlive: true
    },
    component: () => import('@/views/UserData.vue')
  },
  {
    path: '/my/editPassword',
    name: 'EditPassword',
    meta: {
      login: true,
      hideTab: true,
      notKeepAlive: true
    },
    component: () => import('@/views/EditPassword.vue')
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      hideTab: true,
      notKeepAlive: true
    },
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/search',
    name: 'Search',
    meta: {
      hideTab: true
    },
    component: () => import('@/views/Search.vue')
  },
  {
    path: '*',
    name: 'Error',
    component: () => import('@/views/Error.vue')
  },
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.login && !store.getters.tokens) {
    next('/login');
  } else {
    next();
  }
})
export default router
